package cn.picclife.mwx.manager.sys.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.picclife.mwx.common.core.util.crypto.PasswordHashUtil;
import cn.picclife.mwx.manager.sys.mapper.OperatorMapper;
import cn.picclife.mwx.manager.sys.mapper.RolesMapper;
import cn.picclife.mwx.manager.sys.model.bo.BranchDataAuthBo;
import cn.picclife.mwx.manager.sys.model.bo.CommonBo;
import cn.picclife.mwx.manager.sys.model.bo.OperatorMenuInfo;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.RolesBo;
import cn.picclife.mwx.manager.sys.model.bo.User;
import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import cn.picclife.mwx.manager.sys.model.po.DataAuth;
import cn.picclife.mwx.manager.sys.model.po.DataAuthType;
import cn.picclife.mwx.manager.sys.model.po.Enum;
import cn.picclife.mwx.manager.sys.model.po.Operator;
import cn.picclife.mwx.manager.sys.service.UserMgMtService;
import cn.picclife.mwx.manager.util.uniqueseq.UniqueSeq;


/**
 * @ClassName: UserMgMtServiceImpl
 * @Description:用户管理service实现层
 * @author hui
 * @date: 2016年12月8日 上午10:54:58 <br/>
 */
@Service
public class UserMgMtServiceImpl implements UserMgMtService {
	@Autowired
	private OperatorMapper operatorMapper;
	
	@Autowired
	private RolesMapper rolesMapper;
	
	@Override
	public int modifyUser(HttpServletRequest req, HttpServletResponse res, Operator operator) {
		int row= operatorMapper.updateByPrimaryKeySelective(operator);
		return row;
	}

	@Override
	public int deleteUser(HttpServletRequest req, HttpServletResponse res, CommonBo commonBo) {
		operatorMapper.deleteOperatorRoles(commonBo);
		operatorMapper.deleteUserBranchs(commonBo);
		int row= operatorMapper.deleteByPrimaryKey(commonBo);
		return row;
	}

	@Override
	public int addUser(HttpServletRequest req, HttpServletResponse res, Operator user) {
		int row= operatorMapper.insert(user);
		return row;
	}

	@Override
	public PageCount queryUser(HttpServletRequest req, HttpServletResponse res, PageCount pageCount) {
		int allCount = operatorMapper.queryUserAllCount(pageCount.getObject());
		int allPages = allCount/pageCount.getRowNum();
		if (allCount%pageCount.getRowNum() > 0) {
			allPages += 1; 
		}
		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		List<User> operatorList = operatorMapper.queryUser(pageCount);
		pageCount.setDatas(operatorList);
		return pageCount;
	}

	@Override
	public Operator selectByOptnoAndPwd(String optNo,char[] password) {
		Operator operator = operatorMapper.selectByPrimaryKey(optNo);
		if (operator != null) {
			boolean validatePassword = PasswordHashUtil.validatePassword(password, String.valueOf(operator.getOptPassword()));
			if (!validatePassword) {
				return null;
			}
		}
		return operator;
	}

	@Override
	public List<OperatorMenuInfo> queryUserMenuList(Operator operator) {
		List<OperatorMenuInfo> userMenuList = operatorMapper.queryUserMenuList(operator);
		return userMenuList;
	}

	@Override
	public List<BranchDataAuthBo> queryUserDeptListByOptno(String branchId,boolean isStub) {
		List<BranchDataAuthBo> userDeptList;
		if(isStub){
			userDeptList=operatorMapper.queryUserDeptStubList(branchId);
		}else {
			userDeptList = operatorMapper.queryUserDeptList(branchId);
		}
		return userDeptList;
	}

	@Override
	public DataAuthType queryAuthType() {
		DataAuthType authTypeMap = operatorMapper.queryAuthType();
		return authTypeMap;
	}
	@Override
	public int addRoleUser(CommonBo commonBo, String user) {
		int num = 0;
		for (Map<String, Object> userRole : commonBo.getDatas()) {
			RolesBo rolesUserBo = new RolesBo();
			Long serno = UniqueSeq.getUniqueSeq("optro_id", "t_operator_roles");
			rolesUserBo.setRoleNo(userRole.get("roleNo").toString());
			rolesUserBo.setOptNo(commonBo.getObject().get("optNo").toString());
			rolesUserBo.setCrtDate(new Date());
			rolesUserBo.setRoleId(serno);
			rolesUserBo.setCrtUser(user);
			rolesUserBo.setDataFlag("1");
			rolesUserBo.setStartDate(new Date());
			num += rolesMapper.deleteRolesUser(rolesUserBo);
			if ("1".equals(userRole.get("checked").toString())|| "true".equals(userRole.get("checked").toString())) {
				num += rolesMapper.saveRolesUser(rolesUserBo);
			}
		}
		return num;
	}

	@Override
	public int addUserDataAuth(CommonBo commonBo,String user) {
		int num = 0;
		operatorMapper.deleteUserDept(commonBo.getObject().get("optNo").toString());	
		List<DataAuth> userDataAuth=new ArrayList<DataAuth>();
		Date date=new Date();
		if (commonBo.getDatas()!=null){
		for (Map<String, Object> dataAuth : commonBo.getDatas()) {
			DataAuth auth=new DataAuth();
			Long serno = UniqueSeq.getUniqueSeq("auth_id", "t_data_auth");
			auth.setSerno(serno.toString());
			auth.setObjectNo(commonBo.getObject().get("optNo").toString());
			auth.setObjectType("USER");
			auth.setDataAuthNo(dataAuth.get("dataAuthNo").toString());
			auth.setIsHalfCheck(dataAuth.get("isHalfCheck").toString());
			auth.setDataAuthType("DEPT");
			auth.setStartDate(date);
			auth.setCrtDate(date);
			auth.setCrtUser(user);
			auth.setDataFlag("1");
			userDataAuth.add(auth);
		}
		
		num = operatorMapper.addUserDataAuth(userDataAuth);
		}else{
			num =-1;
		}
		return num;
	}

	@Override
	public List<Map<String, Object>> getUserDataAuth(String userNo, String optNo) {
		List<Map<String, Object>> userDataAuth = operatorMapper.getUserDataAuth(userNo,optNo);
		return userDataAuth;
	}

	@Override
	public List<String> getUserDeptList(String optNo) {
		List<String> userDept = operatorMapper.getUserDept(optNo);
		return userDept;
	}

	@Override
	public Operator getUserDetail(String optNo) {
		Operator operator = operatorMapper.selectUserDetail(optNo);
		return operator;
	}
	
	@Override
	public PageCount queryUserRoles(PageCount pageCount) {
		// RolesBo roleBo = (RolesBo)pageCount.getObject();
		String optId=operatorMapper.queryOptId(pageCount.getObject().get("optNo"));
		pageCount.getObject().put("optId", optId);
		int allCount = operatorMapper.queryUserRolesAllCount(pageCount.getObject());
		int allPages = allCount / pageCount.getRowNum();

		if (allCount % pageCount.getRowNum() > 0) {
			allPages += 1;
		}

		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		List<Map<String, Object>> rolesList = operatorMapper.queryUserRoles(pageCount);
		pageCount.setDatas(rolesList);
		return pageCount;
	}

	@Override
	public List<String> queryBranchList(String opt_no) {
		
		return operatorMapper.queryBranchList(opt_no);
	}

	@Override
	public UserSessionBo queryBranchByOptNo(String optNo) {
		return operatorMapper.queryBranchByOptNo(optNo);
	}

	@Override
	public List<Enum> queryEnumInfo(String enumType) {
		return operatorMapper.queryEnumInfo(enumType);
	}

	@Override
	public int modifyUserPwd(Operator operator) {
		return operatorMapper.updateByPrimaryKeySelective(operator);
	}

	@Override
	public List<Enum> queryOptType() {
		return operatorMapper.queryOptType();
	}

	@Override
	public List<BranchDataAuthBo> queryUserDeptListByOptnoAuth(String branchId, boolean isStub, String opt_no) {
		List<BranchDataAuthBo> userDeptList;
		if(isStub){
			userDeptList=operatorMapper.queryUserDeptStubListAuth(branchId,opt_no);
		}else {
			userDeptList = operatorMapper.queryUserDeptListAuth(branchId,opt_no);
		}
		return userDeptList;
	}
}
